package cn.wolfcode.rbac.web.interceptor;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
//import jdk.internal.org.objectweb.asm.commons.Method;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.List;

public class PermissionInterceptor implements HandlerInterceptor {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IRoleService roleService;
    @Override
    /**
     * 0.获取用户信息
     * 1.是超级管理员 true
     * 2.方法没有@RequirePermisson true
     * 3.有权限 true
     * 4.return redirect:/common/nopermission.jsp
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //不拦截静态资源的加载
//        if (handler instanceof DefaultServletHttpRequestHandler){
//            return true;
//        }
//
//        HttpSession session=request.getSession();
//        List<Permission> permissions=(List<Permission>)session.getAttribute("EXPRESSION_IN_SESSION");
//
//        Employee employee=(Employee) session.getAttribute("USER_IN_SESSION");
//
//        HandlerMethod handlerMethod=(HandlerMethod) handler;
//        RequiredPermission anno= handlerMethod.getMethodAnnotation(RequiredPermission.class);
//        if(employee!=null&&employee.getAdmin()!=null&&employee.getAdmin()) {
//            return true;
//        }
//        if(anno==null) {
//            return true;
//        }
//        if(permissions.contains(anno.expression())) {
//            return true;
//        }
//        request.getRequestDispatcher("/WEB-INF/views/common/nopermission.jsp").forward(request,response);
//        return false;
        return true;
    }

}
